let app = getApp();
Page({
  data: {
    CustomBar: app.globalData.CustomBar,
    intervalTime: 20000, // 刷新间隔时间
  },
  onLoad: function(options) {
    let that = this;
    that.setData({
      stationArr: [{
          linename: '水拥坑(海天公园)'
        },
        {
          linename: '0'
        },
        {
          linename: '中大五院'
        },
        {
          linename: '0'
        },
        {
          linename: '华子石东'
        },
        {
          linename: '0'
        },
        {
          linename: '梅华东'
        },
        {
          linename: '0'
        },
        {
          linename: '大镜山'
        },
        {
          linename: '0'
        },
        {
          linename: '兴业路口'
        },
        {
          linename: '0'
        },
        {
          linename: '古元美术馆'
        },
        {
          linename: '0'
        },
        {
          linename: '迎宾北路口'
        },
        {
          linename: '0'
        },
        {
          linename: '香山驿站'
        },
        {
          linename: '0'
        },
        {
          linename: '梅华中'
        },
        {
          linename: '0'
        },
        {
          linename: '珠海一中'
        },
        {
          linename: '0'
        },
        {
          linename: '蓝盾路口'
        },
        {
          linename: '0'
        },
        {
          linename: '上冲小镇'
        },
      ],
    })
    wx.createSelectorQuery().select('.nowMsgBoxFarther').boundingClientRect(function(rect) {
      that.setData({
        nowMsgBoxHeight: rect.height
      })
    }).exec();
  },
  // 本页重新展示
  onShow: function() {
    let that = this;
    console.log("来了Tram");
    let intervalNum = setInterval(e => {
      that.geNowTram();
    }, that.data.intervalTime)
    that.setData({
      intervalNum
    })
    that.geNowTram();
  },
  // 本页被隐藏
  onHide: function() {
    let that = this;
    console.log("去了Tram");
    clearInterval(that.data.intervalNum);
  },
  // 查询实时电车信息
  geNowTram: function() {
    let that = this,
      leftDataArr = {
        url: 'http://wx.zhmrt.com:13555/Inquire/Index/left_route.html',
        what: "Bus"
      },
      rightDataArr = {
        url: 'http://wx.zhmrt.com:13555/Inquire/Index/right_route.html',
        what: "Bus"
      },
      leftData = '',
      rightData = '',
      nowArr = app.coolCopy(this.data.stationArr);
    that.setData({
      refreshHidden: 'ok',
    })
    setTimeout(e => {
      that.setData({
        refreshHidden: '',
      })
    }, 2500)
    app.reGetFn(app.globalData.url, leftDataArr).then(e => {
      leftData = e.data.leftRoute;
      for (let k in leftData)
        for (let i in nowArr)
          if (leftData[k].vehicle == i) {
            nowArr[i].leftTram = true;
            continue;
          }
      app.reGetFn(app.globalData.url, rightDataArr).then(e => {
        rightData = e.data.rightRoute;
        for (let k in rightData)
          for (let i in nowArr)
            if (rightData[k].vehicle == i) {
              nowArr[i].rightTram = true;
              continue;
            }
        that.setData({
          nowTramArr: nowArr
        })
        console.log("电车查询一次", nowArr)
      })
    })
  },
})